layui.define(['jquery', 'element', 'http'],
    function (exports) {

        var $ = layui.jquery;
        var http = layui.http;
        var table = layui.table;
        var treeTable = layui.treeTable;
        var tools = new function () {

            /**
             * @since 防抖算法 
             * 
             * @param fn 要执行的方法
             * @param time 防抖时间参数
             */
            this.debounce = function (fn, time) {
                var timer = null
                return function () {
                    var arguments = arguments[0]
                    if (timer) {
                        clearTimeout(timer)
                    }
                    timer = setTimeout(function () {
                        fn(arguments)
                    }, time)
                }
            }

            // image 转 base64
            this.imageToBase64 = function (img) {
                var canvas = document.createElement("canvas");
                canvas.width = img.width;
                canvas.height = img.height;
                var ctx = canvas.getContext("2d");
                ctx.drawImage(img, 0, 0, img.width, img.height);
                var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
                var dataURL = canvas.toDataURL("image/" + ext);
                return dataURL;
            }

            //表格初始化
            this.PageTable = function (userOptions) {
                table.set({
                    headers: { Authorization: http.getToken() }
                });
                //
                userOptions = userOptions || {};
                var defaultOpts = {
                    elem: '#lsttable',
                    toolbar: '#toolbar',
                    page: true,
                    method: 'POST',
                    contentType: 'application/json',
                    height: 'full-100',
                    limit: 20,
                    limits: [10, 20, 50, 100, 200],
                    defaultToolbar: [
                        'filter',
                        'exports',
                        'print'
                    ],
                    request: {
                        pageName: 'page',
                        limitName: 'pagesize'
                    },
                    parseData: function (res) {
                        return {
                            "count": res.result.total,
                            'statusCode': res.code,
                            'data': res.result.items
                        };
                    },
                    response: {
                        statusName: 'statusCode',
                        statusCode: 200
                    }
                }
                var options = $.extend(true, {}, defaultOpts, userOptions);
                table.render(options);
            }
            this.ListTable = function (userOptions) {
                table.set({
                    headers: { Authorization: http.getToken() }
                });
                //
                userOptions = userOptions || {};
                var defaultOpts = {
                    elem: '#lsttable',
                    toolbar: '#toolbar',
                    page: false,
                    method: 'GET',
                    defaultToolbar: [{
                        layEvent: 'refresh', icon: 'layui-icon-refresh'
                    },
                        'exports',
                        'filter'
                    ],
                    parseData: function (res) {
                        return {
                            'statusCode': res.code,
                            'data': res.result
                        };
                    },
                    response: {
                        statusName: 'statusCode',
                        statusCode: 200
                    }
                }
                var options = $.extend(true, {}, defaultOpts, userOptions);
                table.render(options);
            }
            //树形表格
            this.TreeTable = function (userOptions) {
                table.set({
                    headers: { Authorization: http.getToken() }
                });
                //
                userOptions = userOptions || {};
                var defaultOpts = {
                    elem: '#lsttable',
                    toolbar: '#toolbar',
                    parseData: function (res) {
                        return {
                            'statusCode': res.code,
                            'data': res.result
                        };
                    },
                    response: {
                        statusName: 'statusCode',
                        statusCode: 200
                    }
                }
                var options = $.extend(true, {}, defaultOpts, userOptions);
                treeTable.render(options);
            }

            //打开窗口
            this.open = function (userOptions) {
                userOptions = userOptions || {};
                defaultOpts = {
                    type: 2,
                    maxmin: false,
                    shadeClose: false,
                    offset: 'r',
                    anim: 'slideLeft', // 从右往左
                    btn: ['保存', '取消']
                }
                var options = $.extend(true, {}, defaultOpts, userOptions);
                layer.open(options);
            }
            //确认提示
            this.confirm = function (msg, callback) {
                return layer.confirm(msg, { title: '系统提示', icon: 3, area: ['340px', '160px'] }, callback);
            }
            //弹出层赋值
            this.initValue = function (layero, data) {
               var iframeWin = window[layero.find('iframe')[0]['name']];
               let t = setInterval(function () {                  
                   if (typeof (iframeWin.app.initData) == 'function') {
                       clearInterval(t);
                       iframeWin.app.initData(data);
                   }
                }, 100);               
            }
        };

        exports('tools', tools);
    })
